package com.shop.cereshop.app.dl.logic.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.app.dao.dl.*;
import com.shop.cereshop.app.dao.product.CereProductImageDAO;
import com.shop.cereshop.app.dao.product.CereProductSkuDAO;
import com.shop.cereshop.app.dao.product.CereShopProductDAO;
import com.shop.cereshop.app.dl.logic.domain.CareShopColumn;
import com.shop.cereshop.app.dl.logic.domain.CareShopNews;
import com.shop.cereshop.app.dl.logic.domain.CareShopNewsColumn;
import com.shop.cereshop.app.dl.logic.domain.CereProductSku;
import com.shop.cereshop.app.dl.logic.service.CareShopNewsService;
import com.shop.cereshop.app.dl.logic.utils.ListPageHelper;
import com.shop.cereshop.app.dl.logic.utils.ResultInfo;
import com.shop.cereshop.app.dl.logic.vo.News;
import com.shop.cereshop.app.redis.service.api.SetRedisService;
import com.shop.cereshop.commons.domain.product.CereProductImage;
import com.shop.cereshop.commons.domain.product.CereShopProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: dlshop
 * 描述：
 * @author: 我好帅
 * @create: 2021-06-15 15:40
 **/
@Service
@Transactional
public class CareShopNewsServiceImpl implements CareShopNewsService {
    @Autowired
    private CareShopNewsMapper careShopNewsMapper;
    @Autowired
    private CareShopColumnMapper careShopColumnMapper;
    @Autowired
    private SetRedisService setRedisService;
    @Autowired
    private CareShopNewsDAO careShopNewsDao;
    @Autowired
    private CareShopNewsColumnDAO careShopNewsColumnDao;

    @Override
    public int deleteByPrimaryKey(Long newsId) {
        return careShopNewsDao.deleteByPrimaryKey(newsId);
    }

    @Override
    public int insert(com.shop.cereshop.app.dl.logic.domian.CareShopNews record) {
        return careShopNewsDao.insert(record);
    }

    @Override
    public int insertSelective(com.shop.cereshop.app.dl.logic.domian.CareShopNews record) {
        return careShopNewsDao.insertSelective(record);
    }
    @Autowired
    private CereShopProductDAO cereShopProductDAO;
    @Autowired
    private CereProductSkuDAO cereProductSkuDAO;
    @Autowired
    private CereProductImageDAO cereProductImageDAO;
    @Override
    public com.shop.cereshop.app.dl.logic.domian.CareShopNews selectByPrimaryKey(Long newsId) {
        com.shop.cereshop.app.dl.logic.domian.CareShopNews news = careShopNewsDao.selectByPrimaryKey(newsId);
        Long productId = news.getProductId();
        if (productId!=null){
            CereShopProduct shopProduct = cereShopProductDAO.selectByPrimaryKey(productId);
            com.shop.cereshop.commons.domain.product.CereProductSku skuObj =cereProductSkuDAO.selectByproductId(productId);
            CereProductImage cc = cereProductImageDAO.getById(productId);
            skuObj.setSkuImage(cc.getProductImage());
        news.setSkuObj(skuObj);
        news.setShopProduct(shopProduct);
        }
        return news;
    }

    @Override
    public int updateByPrimaryKeySelective(com.shop.cereshop.app.dl.logic.domian.CareShopNews record) {
        return careShopNewsDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(com.shop.cereshop.app.dl.logic.domian.CareShopNews record) {
        return careShopNewsDao.updateByPrimaryKey(record);
    }

    @Override
    public int delNews(Long... newsIds) {
        int i1 = 0;
        for (int i = 0; i < newsIds.length; i++) {
            i1 += careShopNewsDao.deleteByPrimaryKey(newsIds[i]);
        }
        return i1;
    }

    @Override
    public PageInfo getNewsList(Long columnId, Integer page, Integer row) {
        PageHelper.startPage(page, row);
        //查询所有对应栏目下的新闻
        List<com.shop.cereshop.app.dl.logic.domian.CareShopNewsColumn> list = careShopNewsColumnDao.getNewsList(columnId);
        ArrayList<com.shop.cereshop.app.dl.logic.domian.CareShopNews> careShopNews = new ArrayList<>();
        //通过新闻id找出新闻具体
        for (com.shop.cereshop.app.dl.logic.domian.CareShopNewsColumn careShopNewsColumn : list) {
            com.shop.cereshop.app.dl.logic.domian.CareShopNews careShopNews1 = careShopNewsDao.selectByPrimaryKey(careShopNewsColumn.getNewsId());
            if (careShopNews1 != null) {
                //添加到集合中
                careShopNews.add(careShopNews1);
            }
        }

        return new PageInfo(careShopNews);
    }

    @Override
    public ResultInfo selectByColumn(Integer page, Integer row, Integer columnId, String content) {
//        PageHelper.startPage(page, row);
//        if (content != null && !content.equals("")) {
//            setRedisService.sSet("searchHistory", content);
//        }
        //columnId 1周边景区推荐, 2 文章列表
        //1对多, 1个栏目id, 获取多个新闻id
        CareShopColumn careShopColumn = careShopColumnMapper.selectByPrimaryKey(Long.valueOf(columnId));
        List<CareShopNewsColumn> list = careShopColumn.getNewsColumn();
        List<News> news = new ArrayList<>();
        News n = new News();
        for (CareShopNewsColumn c : list) {
            CareShopNews careShopNews = careShopNewsMapper.serachByIdAndContent(c.getNewsId(), content);
            if (careShopNews != null) {
                n.setNewsId(careShopNews.getNewsId());
                if (c.getColumnid()==1){
                    n.setMapPath(careShopNews.getColumnImageUrl());
                }else {
                    n.setMapPath(careShopNews.getMapPath());
                }
                n.setTitle(careShopNews.getTitle());
                if (columnId == 2) {
                    n.setPubTime(careShopNews.getPubTime());
                }
                news.add(n);
            }
            n = new News();
        }
        int size = news.size();
        int total = size / row + 1;
        if (page > total) {
            return new ResultInfo(200, "没有更多资讯");
        }
        PageInfo<News> pageInfo1 = ListPageHelper.startPage(news, page, row);
        //list分页
//        PageInfo pageInfo = new PageInfo(news);
        return new ResultInfo(pageInfo1);
    }
}


